#coding:utf8

import winrawin
import tkinter as tk

import time
import queue
import threading

q = queue.Queue()

last_move_time = 0
is_moving = False
start_t = 0

gdx = 0
gdy = 0
g_m = {}
from collections import deque
q1 = deque()
q2 = deque()
left_m =  1056573789
gg = 0.2
mouse_delay = 0.3

def deal_input(a):
    global  is_moving, start_t , last_move_time,g_m, q1, q2
    h, x, y , t= a
    last_move_time = t
    if not is_moving:
        q1 = deque()
        q2 = deque()       
        is_moving = True
        start_t = t
        #print('start', t)
        return
    
    if h == left_m:
        q1.append((x, y, t))
    else:
        q2.append((x, y, t))

def squash_q(qq, t2) :
    tt = 0
    dx = 0
    dy = 0    
    while len(qq) > 0 and qq[0][2] <= t2:
        x, y, t = qq.popleft()
        dx += x
        dy += y
        tt = t
    if tt > 0:
        qq.appendleft((dx, dy, tt))
    
def cut_q(qq, t2) :
    tt = 0
    dx = 0
    dy = 0    
    while len(qq) > 0 and qq[0][2] <= t2:
        x, y, t = qq.popleft()
        dx += x
        dy += y
        tt = t
    return dx, dy, tt

import math

def deal_ab(a, b):
    global to
    x, y, t = a
    d1 = math.sqrt(x * x + y * y)
    if d1< min_d:
        return
    du = math.atan2(y, x)
    x, y, t = b
    d2 = math.sqrt(x * x + y * y)
    if d2< min_d:
        return
    
    du2 = math.atan2(y, x)
    d = du2 - du
    if d >  math.pi:
        d -= 2 * math.pi
    if d < - math.pi:
        d += 2 * math.pi
    if abs(d) < math.pi * 0.5:
        return
    a = -1
        
    x, y, t = b
    if abs(x) > abs(y):
        if x > 0:
            a = 1
    else:
        if y > 0:
            a = 1
    #print('get', a, max(d1, d2))
    to += a * max(d1, d2)
    print(to)
            
def int_cut():
    if len(q1) == 0 or len(q2) == 0:
        return
    
    if q1[-1][2] - q1[0][2] < 2 * gg:
        return
    
    if q1[-1][2] - q1[0][2] < 2 * gg:
        return
    

     
def deal_sleep():
    global  is_moving, start_t , last_move_time, gdx, gdy, g_m
    if is_moving and time.time() - last_move_time > mouse_delay:
        is_moving = False
        #print('stop', time.time())
        
    if len(q1) == 0 and len(q2) == 0:
        return
    
    if len(q2) == 0:
        cut_q(q1,time.time()-gg)
        return
    
    if len(q1) == 0:        
        cut_q(q2, time.time()-gg)
        return
    
    t1 = q1[-1][2]
    t2 = q2[-1][2]
    
    tm = min(t1, t2)
    tma = max(t1, t2)
    
    if abs(t1-t2) < gg:
        if time.time() - tma < gg:
            squash_q(q1, tm)
            squash_q(q2, tm)
            int_cut()
            return
        else:
            a = cut_q(q1, time.time())
                 
            b = cut_q(q2, time.time())
           
            deal_ab(a, b)
        return
    
    a = cut_q(q1, tm+gg)
    
    b = cut_q(q2, tm+gg)
    
    deal_ab(a, b)
    
    cut_q(q1, time.time()-gg)
    cut_q(q2, time.time()-gg)
    
def one_loop():
    dx1 = 0
    dy1 = 0
    dx2 =0
    dy2 = 0    
    while not q.empty():               
        a = q.get()        
        h, x, y, t = a
        if h == left_m:
            dx1 += x
            dy1 += y
        else:
            dx2 += x
            dy2 += y
    return dx1, dy1, dx2, dy2
min_d = 20
to = 0
co = 0
def aa(a):
    global to , co
    co += 1
    x1, y1, x2, y2 = a
    xx = x1 + x2
    yy = y1 + y2
    d3 = math.sqrt(xx * xx + yy * yy)
    d1 = math.sqrt(x1 * x1 + y1 * y1)
    d2 = math.sqrt(x2 * x2 + y2 * y2)
    du = math.atan2(y1, x1)
    du2 = math.atan2(y2, x2)
    d = du2 - du
    if d >  math.pi:
        d -= 2 * math.pi
    if d < - math.pi:
        d += 2 * math.pi
    if abs(d) > 2:       
        print(co, d1, d2, d3, d)
    
    if d3 > max(d2 , d1):
        return
    if d1 < 5 or d2 < 5:
        return
    if d3 > 200:
        return
    if abs(d) < 2:
        return    
    a = -1
    
    if abs(x2) > abs(y2):
        if x2 > 0:
            a = 1
    else:
        if y2 > 0:
            a = 1    
    return a * max(d1, d2)

def thrf():
    global  is_moving, start_t , last_move_time, gdx, gdy, g_m, to
    q = deque([], maxlen=3)
    while True:
        time.sleep(0.15)
        a = one_loop()       
        dd = aa(a)       
        if dd == None:
            q.append([0, 0, False])
            continue
        found = False
        for i in range(len(q)):
            r, d, hasAdd = q[i]
            if r:
                found = True
                if not hasAdd:
                    q[i][2] = True
                    to += d
                    print(round(abs(to) / 5) *'*' )
        if found:
            q.append([1,dd, True])
            to += dd
            print(round(abs(to) / 5) *'*' )
        else:
            q.append([1, dd, False])            
            
thr = threading.Thread(target=thrf)
thr.daemon = True
thr.start()



def handle_event(e: winrawin.RawInputEvent):    
    if e.event_type != 'move':
        return
    q.put((e.device.handle, e.delta_x, e.delta_y, time.time()))
    

window = tk.Tk()
winrawin.hook_raw_input_for_window(window.winfo_id(), handle_event)
window.mainloop()